package com.jjclub.esDao;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.rowset.serial.SerialArray;
import java.io.IOException;
import java.util.Map;

/**
 * @Classname MarryDao
 * @Description TODO
 * @Date 2019/12/11 17:59
 * @Created by jq
 */
@Repository
public class MarryDao {

    private final static Logger logger = LoggerFactory.getLogger(MarryDao.class);

    @Autowired
    private RestHighLevelClient esClient;

    public void deal() {
        //声明一个search请求类
        SearchRequest searchRequest = new SearchRequest("rc_marry2");
        //创建一个searchSource构造类
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 设置搜索，可以是任何类型的 QueryBuilder
        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("name_man", "张三ss"));
        // 将 SearchSourceBuilder  添加到 SeachRequest 中
        searchRequest.source(searchSourceBuilder);
        // 设置搜索的 type
        searchRequest.types("marry");
        SearchResponse searchResponse = null;
        try {
            searchResponse = esClient.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
            for(SearchHit searchHit:searchHits){
                System.out.println(searchHit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
